package com.agileboot.domain.search.db;

import com.agileboot.domain.search.dto.HotSearchDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * (SearchHistroy)表服务实现类
 *
 * @author makejava
 * @since 2023-12-12 11:22:20
 */
@Service
public class SearchHistoryServiceImpl extends ServiceImpl<SearchHistoryMapper, SearchHistory> implements SearchHistoryService {

    @Autowired
    private SearchHistoryMapper searchHistoryMapper;

    @Override
    public List<String> getSearchKey(Long userId) {

        LambdaQueryWrapper<SearchHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SearchHistory::getUserId,userId);
        queryWrapper.orderByDesc(SearchHistory::getCreateTime);
        queryWrapper.last("limit 10");

        List<SearchHistory> searchHistories = searchHistoryMapper.selectList(queryWrapper);
        return searchHistories.stream().map(SearchHistory::getSearchWord).collect(Collectors.toList());
    }

    @Override
    public List<String> getHotSearchMedicineList() {
        List<HotSearchDTO> hotSearchDTOS = searchHistoryMapper.getHotSearchMedicineList();
        if (CollectionUtils.isNotEmpty(hotSearchDTOS)){
            List<String> collect = hotSearchDTOS.stream().map(HotSearchDTO::getSearchWord).collect(Collectors.toList());
            return collect;
        }
        return null;
    }
}

